package com.oak.interviewdemo.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oak.interviewdemo.common.CommonConstant;
import com.oak.interviewdemo.sys.entity.User;
import com.oak.interviewdemo.sys.mapper.UserMapper;
import com.oak.interviewdemo.sys.service.IUserService;
import com.oak.interviewdemo.utils.JwtUtil;
import jdk.nashorn.internal.runtime.GlobalConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guoxy
 * @since 2020-05-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<User> selectUserPage(Integer current,Integer pageSize,User user) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.like("username", user.getUsername());
        Page<User> page = new Page<>(current, pageSize);
        IPage<User> iPage = userMapper.selectPageVo(page, wrapper);
//        List<User> records = mapIPage.getRecords();
//        records.forEach(System.out::println);
        return iPage;
    }

    @Override
    public boolean checkLogin(String username, String pwd) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        wrapper.eq("password",pwd);
        wrapper.eq("status", CommonConstant.STATUS_NORMAL);
        User user = userMapper.selectOne(wrapper);
        User user2 = userMapper.selectOne(wrapper);
        if(ObjectUtil.isNull(user)){
            return false;
        }
        JwtUtil.sign(user.getUsername(),user.getPassword());
        return true;
    }


}
